package xiao_jin.fantastic_journey.core.init;

import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import xiao_jin.fantastic_journey.FantasticJourney;
import xiao_jin.fantastic_journey.util.ModUtil;

public enum ModItemGroup {
    Ingredients("ingredients"),
    Tools("tools"),
    CombatSupplies("combat_supplies"),
    Blocks("block"),
    FunctionalBlock("functional_block"),
    Sundries("sundries"),
    Contributor("contributor"),
    Food("food");

    public final String name;

    ModItemGroup(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public ItemStack getIcon() {
        return switch (this) {
            case Ingredients -> ModItems.CRYSTAL_FRAGMENTS.getDefaultStack();
            case Tools -> Items.DIAMOND_PICKAXE.getDefaultStack();
            case CombatSupplies -> ModItems.WAND_APPRENTICE.getDefaultStack();
            case Blocks -> ModBlocks.RELICTECH_STONE.asItem().getDefaultStack();
            case FunctionalBlock -> ModBlocks.RELICTECH_TABLE.asItem().getDefaultStack();
            case Sundries -> ModItems.SANDPAPER.getDefaultStack();
            case Contributor -> ModItems.DIAMOND.getDefaultStack();
            case Food -> ModItems.ANCIENT_CHOCOLATE.getDefaultStack();
        };
    }

    private void addItems(ItemGroup.DisplayContext displayContext, ItemGroup.Entries entries) {
        if (!FantasticJourney.ITEM_GROUP_MAP.containsKey(this)) return;

        // Don't foreach / stream.
        for (ItemConvertible item : FantasticJourney.ITEM_GROUP_MAP.get(this)) {
            entries.add(item);
        }
    }


    public static void registerModItemGroup() {
        for (ModItemGroup value : ModItemGroup.values()) {
            Registry.register(Registries.ITEM_GROUP,
                    ModUtil.getIdOfMod(value.name),
                    FabricItemGroup.builder().displayName(ModUtil.translatable("itemGroup", value.name))
                            .icon(value::getIcon).entries(value::addItems).build());
        }
    }
}
